package fun.diaoer.config.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;

import fun.diaoer.common.Contants;
import fun.diaoer.common.JsonResult;
import lombok.extern.slf4j.Slf4j;

/**
 * 失败处理
 * @author Administrator
 *
 */
@Slf4j
@Component
public class DiaoerAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
	
	@Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,AuthenticationException exception) throws IOException, ServletException {
    	log.error("登录失败");
    	JsonResult<String> jr=new JsonResult<>();
		jr.setCode(400);
		jr.setStatus(false);
		jr.setMsg("登录失败");
		Contants.buildLoginResponse(request, response);
		log.error("错误：{}",exception.getStackTrace());
		response.getWriter().write(JSON.toJSONString(jr));
    }

}
